
import 'package:baselib/utils/toast_util.dart';
import 'package:framelib/config/app_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/base_entity.dart';
import 'package:framelib/net/dio_util.dart';
import 'package:dio/dio.dart';
import 'package:framelib/utils/constant_util.dart';
import 'package:baselib/utils/navigator_util.dart';
class RequestManage{
  static final RequestManage _singleton = RequestManage._internal();

  static RequestManage get instance => RequestManage();

  factory RequestManage(){
    return _singleton;
  }

  RequestManage._internal() {
  }

  /**
   * get方法
   */
  Future<BaseEntity> get<T>(
      String url,
  {
    required Function fromJsonFunc,
    String? baseUrl,
    bool? isTempUseCurSetBaseUrl,
    dynamic data,
    Map<String, dynamic>? queryParameters,
    bool? isFormData,
    List<MultipartFile>? multipartFiles,
    CancelToken? cancelToken,
    List<Map<String, dynamic>>? addHeaders,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
    bool? isReturnAll
  })async{
    BaseEntity baseEntity =  await requestData(
        url,
        baseUrl: baseUrl,isTempUseCurSetBaseUrl: isTempUseCurSetBaseUrl,
        methodtype: METHODTYPE.GET,data: data,queryParameters: queryParameters,isFormData: isFormData,
        multipartFiles: multipartFiles,cancelToken: cancelToken,addHeaders: addHeaders,
        onSendProgress: onSendProgress,onReceiveProgress: onReceiveProgress,isReturnAll: isReturnAll
    );

    var resultData = baseEntity.data;
    if(resultData==null)return baseEntity;
    if(resultData is List){
      List<T> fromJsonData = resultData.map((e) => (fromJsonFunc(e) as T)).toList();
      print('---->List fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData;
    }else{
      T fromJsonData = fromJsonFunc(resultData);
      print('---->fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData;
    }
    return baseEntity;
  }
  /**
   * post方法
   */
  Future<BaseEntity> post<T>(
      String url,
      {
        required Function fromJsonFunc,
        String? baseUrl,
        bool? isTempUseCurSetBaseUrl,
        dynamic data,
        Map<String, dynamic>? queryParameters,
        bool? isFormData,
        List<MultipartFile>? multipartFiles,
        CancelToken? cancelToken,
        List<Map<String, dynamic>>? addHeaders,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
        bool? isReturnAll
      })async{

    BaseEntity baseEntity =  await requestData(
        url,
        baseUrl: baseUrl,isTempUseCurSetBaseUrl: isTempUseCurSetBaseUrl,
        methodtype: METHODTYPE.POST,data: data,queryParameters: queryParameters,isFormData: isFormData,
        multipartFiles: multipartFiles,cancelToken: cancelToken,addHeaders: addHeaders,
        onSendProgress: onSendProgress,onReceiveProgress: onReceiveProgress,isReturnAll: isReturnAll
    );

    var resultData = baseEntity.data;
    if(resultData==null)return baseEntity;
    if(resultData is List){
      List<T> fromJsonData = resultData.map((e) => (fromJsonFunc(e) as T)).toList();
      print('---->List fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData;
    }else{
      T fromJsonData = fromJsonFunc(resultData) as T;
      print('---->fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData ;
    }
    return baseEntity;
  }

  /**
   * post方法
   */
  Future<BaseEntity> put<T>(
      String url,
      {
        required Function fromJsonFunc,
        String? baseUrl,
        bool? isTempUseCurSetBaseUrl,
        dynamic data,
        Map<String, dynamic>? queryParameters,
        bool? isFormData,
        List<MultipartFile>? multipartFiles,
        CancelToken? cancelToken,
        List<Map<String, dynamic>>? addHeaders,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
        bool? isReturnAll
      })async{

    BaseEntity baseEntity =  await requestData(
        url,
        baseUrl: baseUrl,isTempUseCurSetBaseUrl: isTempUseCurSetBaseUrl,
        methodtype: METHODTYPE.PUT,data: data,queryParameters: queryParameters,isFormData: isFormData,
        multipartFiles: multipartFiles,cancelToken: cancelToken,addHeaders: addHeaders,
        onSendProgress: onSendProgress,onReceiveProgress: onReceiveProgress,isReturnAll: isReturnAll
    );

    var resultData = baseEntity.data;
    if(resultData==null)return baseEntity;
    if(resultData is List){
      List<T> fromJsonData = resultData.map((e) => (fromJsonFunc(e) as T)).toList();
      print('---->List fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData;
    }else{
      T fromJsonData = fromJsonFunc(resultData) as T;
      print('---->fromJson=${fromJsonData}');
      baseEntity.data = fromJsonData ;
    }
    return baseEntity;
  }

  Future<BaseEntity> requestData(
      String url,
      {
        String? baseUrl,
        bool? isTempUseCurSetBaseUrl,
        METHODTYPE? methodtype,
        dynamic data,
        Map<String, dynamic>? queryParameters,
        bool? isFormData,
        List<MultipartFile>? multipartFiles,
        CancelToken? cancelToken,
        List<Map<String, dynamic>>? addHeaders,
        ProgressCallback? onSendProgress,
        ProgressCallback? onReceiveProgress,
        bool? isSetToken,
      bool? isReturnAll
      })async{
    List<Map<String, dynamic>> tempHeaders = [];

    //设置默认token
    if(isSetToken??true)tempHeaders = [{'authorization':'Bearer ${AppConfig.instance.token}'}];

    //继续追加用户请求时设置的header
    if((addHeaders??[]).isNotEmpty)(addHeaders??[]).forEach((element)=>tempHeaders.add(element));
    print('---->data=${data}');
    BaseEntity baseEntity = await DioUtil.instance
        .setBaseUrl(baseUrl: baseUrl,isTempUseCurSetBaseUrl: isTempUseCurSetBaseUrl)
        .request(
        url,methodtype: methodtype,data: data,queryParameters: queryParameters,isFormData: isFormData,
        multipartFiles: multipartFiles,cancelToken: cancelToken,addHeaders: tempHeaders,
        onSendProgress: onSendProgress,onReceiveProgress: onReceiveProgress,isReturnAll: isReturnAll
    );
    if(baseEntity?.code != AppConfig.instance.SUCCESS_CODE){
      // ToastUtil.toast(baseEntity?.msg);
    }
    if(baseEntity?.code == AppConfig.instance.UNAUTHORIZED){
      //关闭所有页面跳转到登录页
      // NavigatorUtil.offAllByName(RouteConfig.loginPage);
    }
    return baseEntity;
  }
}